package org.demo;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.List;

/**
 * 获取内容
 *
 * @author yexi
 */
public class ReciveContent {

    public static void main(String[] args) {

        /*
              创建zookeeper对象
         */
        try {
            ZooKeeper zooKeeper = new ZooKeeper(
                    "localhost:2181",
                    10000,
                    new Watcher() {
                        public void process(WatchedEvent watchedEvent) {
                            System.out.println("成功获取连接");
                        }
                    });

            //2. 从Zookeeper中 获取内容
            //2.1 获取节点
            List<String> list = zooKeeper.getChildren("/demo", false);
            System.out.println(list);
            for (String child : list) {
                byte[] result = zooKeeper.getData("/demo/" + child, false, null);
                System.out.println(new String(result));
            }


        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
